﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using LogLib;

namespace ComClassLibrary
{
    public class FormConfig
    {
        public const int GWL_STYLE = (-16);
        public const int WS_VISIBLE = 0x10000000;

        public IntPtr MainWindowHandle
        {
            private set;
            get;
        }

        [DllImport("user32.dll", SetLastError = true)]
        public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);

        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);

        /// <summary>
        /// 将给定的字符串转换为指定类型的对象值
        /// </summary>
        /// <param name="TheValue"></param>
        /// <param name="ConvertType"></param>
        /// <returns></returns>
        private static object ConvertStrToTypeValue(string TheValue, Type ConvertType, Control ctlNow = null)
        {
            try
            {
                object ConvertValue = null;
                string[] TempParts;
                switch (ConvertType.Name)
                {
                    #region A
                    case "Appearance":
                        Appearance ae;
                        if (Enum.TryParse(TheValue, out ae))
                            ConvertValue = ae;
                        break;
                    case "AccessibleRole":
                        AccessibleRole ar;
                        if (Enum.TryParse(TheValue, out ar))
                            ConvertValue = ar;
                        break;
                    case "AutoValidate":
                        AutoValidate av;
                        if (Enum.TryParse(TheValue, out av))
                            ConvertValue = av;
                        break;
                    case "AutoSizeMode":
                        AutoSizeMode asm;
                        if (Enum.TryParse(TheValue, out asm))
                            ConvertValue = asm;
                        break;
                    case "AutoScaleMode":
                        AutoScaleMode asme;
                        if (Enum.TryParse(TheValue, out asme))
                            ConvertValue = asme;
                        break;
                    case "AutoCompleteMode":
                        AutoCompleteMode acm;
                        if (Enum.TryParse(TheValue, out acm))
                            ConvertValue = acm;
                        break;
                    case "AutoCompleteSource":
                        AutoCompleteSource acs;
                        if (Enum.TryParse(TheValue, out acs))
                            ConvertValue = acs;
                        break;
                    case "AnchorStyles":
                        TempParts = TheValue.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                        AnchorStyles AS = AnchorStyles.None, asTemp;
                        foreach (string tp in TempParts)
                        {
                            Enum.TryParse(tp, out asTemp);
                            AS = AS | asTemp;
                        }
                        ConvertValue = AS;
                        break;
                    #endregion

                    #region B
                    case "Boolean":
                        bool Value_bl;
                        if (Boolean.TryParse(TheValue, out Value_bl))
                            ConvertValue = Value_bl;
                        break;
                    case "BorderStyle":
                        BorderStyle bs;
                        Enum.TryParse(TheValue, out bs);
                        ConvertValue = bs;
                        break;
                    #endregion

                    #region C
                    case "Char":
                        if (TheValue.Length > 0)
                            ConvertValue = TheValue[0];
                        break;
                    case "Color":
                        if (TheValue.ToUpper().IndexOf("RGB") >= 0)//由ARGB值指定
                        {
                            TempParts = TheValue.ToUpper().Split(new string[] { "A", "RGB", "(", ",", ")" }, StringSplitOptions.RemoveEmptyEntries);
                            byte A, R, G, B;
                            if (TempParts.Length == 3)
                            {
                                if (byte.TryParse(TempParts[0].Trim(), out R) && byte.TryParse(TempParts[1].Trim(), out G) && byte.TryParse(TempParts[2].Trim(), out B))
                                    ConvertValue = Color.FromArgb(255, R, G, B);

                            }
                            else if (TempParts.Length == 4)
                            {
                                if (TempParts.Length == 4 && byte.TryParse(TempParts[0].Trim(), out A) && byte.TryParse(TempParts[1].Trim(), out R) && byte.TryParse(TempParts[2].Trim(), out G) && byte.TryParse(TempParts[3].Trim(), out B))
                                    ConvertValue = Color.FromArgb(A, R, G, B);
                            }
                        }
                        else
                            ConvertValue = (Color)new ColorConverter().ConvertFromString(TheValue);
                        break;
                    case "CheckState":
                        CheckState cs;
                        if (Enum.TryParse(TheValue, out cs))
                            ConvertValue = cs;
                        break;
                    case "ComboBoxStyle":
                        ComboBoxStyle cbs;
                        Enum.TryParse(TheValue, out cbs);
                        ConvertValue = cbs;
                        break;
                    case "ContentAlignment":
                        ContentAlignment ca;
                        if (Enum.TryParse(TheValue, out ca))
                            ConvertValue = ca;
                        break;
                    case "CharacterCasing":
                        CharacterCasing cc;
                        if (Enum.TryParse(TheValue, out cc))
                            ConvertValue = cc;
                        break;
                    #endregion

                    #region D
                    case "Decimal":
                        Decimal dl;
                        if (Decimal.TryParse(TheValue.Trim(), out dl))
                            ConvertValue = dl;
                        break;
                    case "Double":
                        Double deNow;
                        if (Double.TryParse(TheValue.Trim(), out deNow))
                            ConvertValue = deNow;
                        break;
                    case "DateTime":
                        DateTime Value_DateTime;
                        if (DateTime.TryParse(TheValue, out Value_DateTime))
                            ConvertValue = Value_DateTime;
                        break;
                    case "DateTimePickerFormat":
                        DateTimePickerFormat dtpf;
                        if (Enum.TryParse(TheValue, out dtpf))
                            ConvertValue = dtpf;
                        break;
                    case "DataGridViewCellBorderStyle":
                        DataGridViewCellBorderStyle dgvcbs;
                        if (Enum.TryParse(TheValue, out dgvcbs))
                            ConvertValue = dgvcbs;
                        break;
                    case "DataGridViewClipboardCopyMode":
                        DataGridViewClipboardCopyMode dgvccm;
                        if (Enum.TryParse(TheValue, out dgvccm))
                            ConvertValue = dgvccm;
                        break;
                    case "DataGridViewEditMode":
                        DataGridViewEditMode dgvem;
                        if (Enum.TryParse(TheValue, out dgvem))
                            ConvertValue = dgvem;
                        break;
                    case "DataGridViewRowHeadersWidthSizeMode":
                        DataGridViewRowHeadersWidthSizeMode dgvrhwsm;
                        if (Enum.TryParse(TheValue, out dgvrhwsm))
                            ConvertValue = dgvrhwsm;
                        break;
                    case "DataGridViewSelectionMode":
                        DataGridViewSelectionMode dcvsm;
                        if (Enum.TryParse(TheValue, out dcvsm))
                            ConvertValue = dcvsm;
                        break;
                    case "DockStyle":
                        DockStyle ds;
                        if (Enum.TryParse(TheValue, out ds))
                            ConvertValue = ds;
                        break;
                    case "DrawMode":
                        DrawMode dm;
                        if (Enum.TryParse(TheValue, out dm))
                            ConvertValue = dm;
                        break;
                    case "DialogResult":
                        DialogResult dr;
                        if (Enum.TryParse(TheValue, out dr))
                            ConvertValue = dr;
                        break;
                    #endregion

                    #region F
                    case "Font":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (TempParts.Length == 2)
                        {
                            string FontName = TempParts[0].Trim();
                            int FontSize;
                            if (Int32.TryParse(TempParts[1].Trim(), out FontSize))
                                ConvertValue = new Font(FontName, FontSize);
                        }
                        break;
                    case "FormStartPosition":
                        FormStartPosition fsp;
                        Enum.TryParse(TheValue, out fsp);
                        ConvertValue = fsp;
                        break;
                    case "FormWindowState":
                        FormWindowState fws;
                        Enum.TryParse(TheValue, out fws);
                        ConvertValue = fws;
                        break;
                    case "FormBorderStyle":
                        FormBorderStyle fbs;
                        if (Enum.TryParse(TheValue, out fbs))
                            ConvertValue = fbs;
                        break;
                    case "FlatStyle":
                        FlatStyle fs;
                        if (Enum.TryParse(TheValue, out fs))
                            ConvertValue = fs;
                        break;
                    case "FixedPanel":
                        FixedPanel fp;
                        if (Enum.TryParse(TheValue, out fp))
                            ConvertValue = fp;
                        break;
                    #endregion

                    #region H
                    case "HorizontalAlignment":
                        HorizontalAlignment ha;
                        Enum.TryParse(TheValue, out ha);
                        ConvertValue = ha;
                        break;
                    #endregion

                    #region I
                    case "ImeMode":
                        ImeMode im;
                        if (Enum.TryParse(TheValue, out im))
                            ConvertValue = im;
                        break;
                    case "InsertKeyMode":
                        InsertKeyMode ikm;
                        if (Enum.TryParse(TheValue, out ikm))
                            ConvertValue = ikm;
                        break;
                    case "Int32":
                        Int32 Value_Int32;
                        if (Int32.TryParse(TheValue, out Value_Int32))
                            ConvertValue = Value_Int32;
                        break;
                    case "Int32[]":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (TempParts.Length > 0)
                        {
                            int[] Int32Arr = new int[TempParts.Length];
                            bool FindError = false;
                            for (int i = 0; i < TempParts.Length; i++)
                            {
                                if (!int.TryParse(TempParts[i], out Int32Arr[i]))
                                {
                                    FindError = true;
                                    break;
                                }
                            }
                            //未发现错误
                            if (!FindError)
                                ConvertValue = Int32Arr;
                        }
                        break;
                    case "Int64":
                        Int64 Value_Int64;
                        if (Int64.TryParse(TheValue, out Value_Int64))
                            ConvertValue = Value_Int64;
                        break;
                    case "Image":
                        ConvertValue = Tools.GetImageFromFile(TheValue);
                        break;
                    case "ImageLayout":
                        ImageLayout il;
                        if (Enum.TryParse(TheValue, out il))
                            ConvertValue = il;
                        break;
                    case "ImageList":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (TempParts.Length > 0)
                        {
                            ImageList imgList = new ImageList();
                            Image imgNow;
                            foreach (string FilePath in TempParts)
                            {
                                imgNow = Tools.GetImageFromFile(FilePath);
                                if (imgNow != null)
                                    imgList.Images.Add(imgNow);
                            }
                            ConvertValue = imgList;
                        }
                        break;
                    case "IntPtr":
                        int ipInt;
                        if (int.TryParse(TheValue, out ipInt))
                            ConvertValue = new IntPtr(ipInt);
                        break;
                    #endregion

                    #region K
                    case "Keys":
                        Keys NowKey;
                        if (Enum.TryParse(TheValue, out NowKey))
                            ConvertValue = NowKey;
                        break;
                    #endregion

                    #region L
                    case "LeftRightAlignment":
                        LeftRightAlignment lra;
                        if (Enum.TryParse(TheValue, out lra))
                            ConvertValue = lra;
                        break;
                    case "LinkArea":
                        LinkArea la;
                        if (Enum.TryParse(TheValue, out la))
                            ConvertValue = la;
                        break;
                    case "LinkBehavior":
                        LinkBehavior lb;
                        if (Enum.TryParse(TheValue, out lb))
                            ConvertValue = lb;
                        break;
                    #endregion

                    #region O
                    case "Orientation":
                        Orientation on;
                        if (Enum.TryParse(TheValue, out on))
                            ConvertValue = on;
                        break;
                    case "ObjectCollection":
                        ConvertValue = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    #endregion

                    #region P
                    case "Point":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        int X, Y;
                        if (TempParts.Length == 2 && Int32.TryParse(TempParts[0].Trim(), out X) && Int32.TryParse(TempParts[1].Trim(), out Y))
                            ConvertValue = new Point(X, Y);
                        break;
                    case "Padding":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        int Left, Top, Right, Bottom;
                        if (TempParts.Length == 4 && int.TryParse(TempParts[0].Trim(), out Left) && int.TryParse(TempParts[1].Trim(), out Top) && int.TryParse(TempParts[2].Trim(), out Right) && int.TryParse(TempParts[3].Trim(), out Bottom))
                            ConvertValue = new Padding(Left, Top, Right, Bottom);
                        break;
                    case "PictureBoxSizeMode":
                        PictureBoxSizeMode pbsm;
                        if (Enum.TryParse(TheValue, out pbsm))
                            ConvertValue = pbsm;
                        break;
                    case "ProgressBarStyle":
                        ProgressBarStyle pbs;
                        if (Enum.TryParse(TheValue, out pbs))
                            ConvertValue = pbs;
                        break;
                    #endregion

                    #region R
                    case "Rectangle":
                        int rgX, rgY, rgWidth, rgHeight;
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (TempParts.Length == 4 && int.TryParse(TempParts[0].Trim(), out rgX) && int.TryParse(TempParts[1].Trim(), out rgY) && int.TryParse(TempParts[2].Trim(), out rgWidth) && int.TryParse(TempParts[3].Trim(), out rgHeight))
                            ConvertValue = new Rectangle(rgX, rgY, rgWidth, rgHeight);
                        break;
                    case "RichTextBoxLanguageOptions":
                        RichTextBoxLanguageOptions rtblo;
                        if (Enum.TryParse(TheValue, out rtblo))
                            ConvertValue = rtblo;
                        break;
                    case "RichTextBoxScrollBars":
                        RichTextBoxScrollBars rtbsb;
                        if (Enum.TryParse(TheValue, out rtbsb))
                            ConvertValue = rtbsb;
                        break;
                    case "RightToLeft":
                        RightToLeft rtl;
                        if (Enum.TryParse(TheValue, out rtl))
                            ConvertValue = rtl;
                        break;
                    #endregion

                    #region S
                    case "Single":
                        Single se;
                        if (Single.TryParse(TheValue, out se))
                            ConvertValue = se;
                        break;
                    case "Size":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        int Width, Height;
                        if (TempParts.Length == 2 && Int32.TryParse(TempParts[0].Trim(), out Width) && Int32.TryParse(TempParts[1].Trim(), out Height))
                            ConvertValue = new Size(Width, Height);
                        break;
                    case "SizeF":
                        TempParts = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        float sfWidth, sfHeight;
                        if (TempParts.Length == 2 && float.TryParse(TempParts[0].Trim(), out sfWidth) && float.TryParse(TempParts[1].Trim(), out sfHeight))
                            ConvertValue = new SizeF(sfWidth, sfHeight);
                        break;
                    case "String":
                        ConvertValue = TheValue;
                        break;
                    case "String[]":
                        ConvertValue = TheValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    case "ScrollBars":
                        ScrollBars sb;
                        if (Enum.TryParse(TheValue, out sb))
                            ConvertValue = sb;
                        break;
                    case "SelectionMode":
                        SelectionMode sm;
                        if (Enum.TryParse(TheValue, out sm))
                            ConvertValue = sm;
                        break;
                    case "SizeGripStyle":
                        SizeGripStyle sgs;
                        if (Enum.TryParse(TheValue, out sgs))
                            ConvertValue = sgs;
                        break;
                    #endregion

                    #region T
                    case "TextImageRelation":
                        TextImageRelation tir;
                        if (Enum.TryParse(TheValue, out tir))
                            ConvertValue = tir;
                        break;
                    case "TabAlignment":
                        TabAlignment ta;
                        if (Enum.TryParse(TheValue, out ta))
                            ConvertValue = ta;
                        break;
                    case "TabAppearance":
                        TabAppearance tae;
                        if (Enum.TryParse(TheValue, out tae))
                            ConvertValue = tae;
                        break;
                    case "TabDrawMode":
                        TabDrawMode tdm;
                        if (Enum.TryParse(TheValue, out tdm))
                            ConvertValue = tdm;
                        break;
                    case "TabSizeMode":
                        TabSizeMode tsm;
                        if (Enum.TryParse(TheValue, out tsm))
                            ConvertValue = tsm;
                        break;
                    case "ToolStripGripStyle":
                        ToolStripGripStyle tsgs;
                        if (Enum.TryParse(TheValue, out tsgs))
                            ConvertValue = tsgs;
                        break;
                    case "ToolStripLayoutStyle":
                        ToolStripLayoutStyle tsls;
                        if (Enum.TryParse(TheValue, out tsls))
                            ConvertValue = tsls;
                        break;
                    case "ToolStripDropDownDirection":
                        ToolStripDropDownDirection tsddd;
                        if (Enum.TryParse(TheValue, out tsddd))
                            ConvertValue = tsddd;
                        break;
                    case "ToolStripRenderMode":
                        ToolStripRenderMode tsrm;
                        if (Enum.TryParse(TheValue, out tsrm))
                            ConvertValue = tsrm;
                        break;
                    case "ToolStripTextDirection":
                        ToolStripTextDirection tstd;
                        if (Enum.TryParse(TheValue, out tstd))
                            ConvertValue = tstd;
                        break;
                    case "TreeViewDrawMode":
                        TreeViewDrawMode tvdm;
                        if (Enum.TryParse(TheValue, out tvdm))
                            ConvertValue = tvdm;
                        break;
                    case "TableLayoutPanelCellBorderStyle":
                        TableLayoutPanelCellBorderStyle tlpcbs;
                        if (Enum.TryParse(TheValue, out tlpcbs))
                            ConvertValue = tlpcbs;
                        break;
                    case "TableLayoutPanelGrowStyle":
                        TableLayoutPanelGrowStyle tlpgs;
                        if (Enum.TryParse(TheValue, out tlpgs))
                            ConvertValue = tlpgs;
                        break;
                    #endregion

                    #region U
                    case "UInt32":
                        UInt32 Value_UInt32;
                        if (UInt32.TryParse(TheValue, out Value_UInt32))
                            ConvertValue = Value_UInt32;
                        break;
                    #endregion
                    default:
                        ConvertValue = TheValue;
                        break;
                }

                return ConvertValue;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 设置给定控件的指定属性值
        /// </summary>
        /// <param name="ctlNow">给定的控件</param>
        /// <param name="propertyName">要设置的属性名称</param>
        /// <param name="propertyValue">要设置的属性值</param>
        /// <returns></returns>
        public static string SetControlProperty(Control ctlNow, string propertyName, object propertyValue)
        {
            //设置控件属性
            if (ctlNow != null && !string.IsNullOrEmpty(propertyName))
            {
                PropertyInfo pi = ctlNow.GetType().GetProperty(propertyName);
                if (pi != null)
                {
                    object FactValue = propertyValue;
                    if (propertyValue != null)
                    {
                        if (propertyValue.GetType().Name == pi.PropertyType.Name)
                            FactValue = propertyValue;
                        else
                            FactValue = ConvertStrToTypeValue(propertyValue.ToString(), pi.PropertyType);
                    }

                    pi.SetValue(ctlNow, FactValue, null);
                }
            }

            return "";
        }

        public static string SetControlAllProperty(Control ctlNow, Dictionary<string, string> dicProperty)
        {
            string str = ctlNow.GetType().ToString();
            foreach (var property in dicProperty)
            {
                if (property.Key == "ControlType")
                {
                    continue;
                }

                //设置控件属性
                if (ctlNow != null)
                {
                    if (property.Key == "Font")  //绑定事件
                    {
                        string[] fontArg = property.Value.Split(',');
                        if (fontArg.Length>2)
                        {
                            //Regular = 0,
                            //Bold = 1,
                            //Italic = 2,
                            //Underline = 4,
                            //Strikeout = 8,
                            FontStyle style = new FontStyle();
                            switch (fontArg[2].ToLower())
                            {
                                case "regular":
                                    style = FontStyle.Regular;
                                    break;
                                case "bold":
                                    style = FontStyle.Bold;
                                    break;
                                case "italic":
                                    style = FontStyle.Italic;
                                    break;
                                case "underline":
                                    style = FontStyle.Underline;
                                    break;
                                case "strikeout":
                                    style = FontStyle.Strikeout;
                                    break;
                                default:
                                    break;
                            }
                            Font font = new Font(fontArg[0], float.Parse(fontArg[1]), style);
                            ctlNow.Font = font;
                        }
                        
                    }
                    else if (property.Key == "Location")
                    {
                        if(ctlNow.GetType().ToString().Contains("ucProgram"))
                            continue;
                        
                        string[] location = property.Value.Split(',');
                        int x = int.Parse(location[0]);
                        int y = int.Parse(location[1]);
                        ctlNow.Location = new Point(x, y);
                    }
                    else //设置属性
                    {
                        PropertyInfo pi = ctlNow.GetType().GetProperty(property.Key);
                        if (pi != null)
                        {
                            object FactValue = property.Value;
                            if (property.Value != null)
                            {
                                if (property.Value.GetType().Name == pi.PropertyType.Name)
                                    FactValue = property.Value;
                                else
                                    FactValue = ConvertStrToTypeValue(property.Value.ToString(), pi.PropertyType);
                            }

                            pi.SetValue(ctlNow, FactValue, null);
                        } 
                    }
                }
            }
            

            return "";
        }

        private IntPtr GetProcessMainWnd(string windowName, string className)
        {
            IntPtr[] FindWindows = SearchWindow.Search(windowName, className);
            if (FindWindows.Length > 0)
            {
                IntPtr MainWnd = FindWindows[0];
                //string strLog = string.Format("查询目标窗口句柄{0}", MainWnd);
                //Log.WriteAsync(strLog);
                return MainWnd;
            }
            return IntPtr.Zero;
        }

        /// <summary>  
        /// 将指定的程序嵌入指定的控件  
        /// </summary>  
        public void EmbedProcess(Process Proc, Control ParentContainer)
        {
            // 获取主窗体句柄  
            if (Proc == null || ParentContainer == null)
                return;
            MainWindowHandle = Proc.MainWindowHandle;
            if (Proc.MainWindowHandle == IntPtr.Zero)
            {
                MainWindowHandle = GetProcessMainWnd(Proc.ProcessName, Proc.ProcessName);
            }
            try
            {
                WinAPI.ShowWindow(MainWindowHandle, (int)ShowCommands.SW_SHOW);

                // 将主窗体嵌入到给定容器控件  
                long ret = SetParent(MainWindowHandle, ParentContainer.Handle);
                if (0==ret)
                {
                    int err = Marshal.GetLastWin32Error();
                    MessageBox.Show("嵌入失败，代码:" + err.ToString());
                }
                
                // 移动主窗体到给定容器控件大小 
                MoveWindow(MainWindowHandle, 0, 0, ParentContainer.Width, ParentContainer.Height, true);
                
                // 隐藏边框                 
                SetWindowLong(new HandleRef(this, MainWindowHandle), GWL_STYLE, WS_VISIBLE);
                
                //设置窗体标题
                //if (WindowTitle != "")
                //{
                //    SendMessage(Proc.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, WindowTitle);
                //}

                ParentContainer.Refresh();
            }
            catch (Exception e)
            {
                Log.WriteException(e.ToString());
            }
        }
    }
}
